package com.zhny.aiot.four.solar.roll.film.v2.plugin;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zhny.aiot.four.solar.roll.film.v2.plugin.model.AdjustableParam;
import com.zhny.aiot.four.solar.roll.film.v2.plugin.model.SolarRollFilm4GV2StoreEntry;
import com.zhny.aiot.four.solar.roll.film.v2.plugin.model.ValueParam;
import com.zhny.aiot.plugin.ICenterFacilities;
import com.zhny.aiot.plugin.IDevice;
import com.zhny.aiot.plugin.core.utils.ConfigUtils;
import com.zhny.aiot.plugin.model.CommandEntry;
import com.zhny.aiot.plugin.model.DeviceEntity;
import com.zhny.aiot.plugin.model.FactoryType;
import com.zhny.aiot.plugin.model.StoreEntity;
import com.zhny.aiot.plugin.zhnygateway.device.ZhnyDeviceFactory;
import com.zhny.aiot.zhny.modbus.embedded.plugin.core.ZhnyModBusServer;
import org.pf4j.Extension;
import org.pf4j.Plugin;
import org.pf4j.PluginWrapper;

import java.util.ArrayList;
import java.util.List;

public class SolarRollFilm4GV2Plugin extends Plugin {
    public SolarRollFilm4GV2Plugin(PluginWrapper wrapper) {
        super(wrapper);
    }

    @Extension
    public static class SolarRollFilm4GV2Factory extends ZhnyDeviceFactory<SolarRollFilm4GV2StoreEntry> {
        @Override
        public List<CommandEntry> supportCommandClasses(){
            String paramJson;String valueJson;
            ObjectMapper mapper = new ObjectMapper();
            mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
            try {
                paramJson = mapper.writeValueAsString(ConfigUtils.getConfigFields(AdjustableParam.class));
                valueJson = mapper.writeValueAsString(ConfigUtils.getConfigFields(ValueParam.class));
            } catch (Exception e) {
                return null;
            }
            List<CommandEntry> commands = new ArrayList<> ();
            commands.add(CommandEntry.valueOf("stop", "停止", "无参数",null));
            commands.add(CommandEntry.valueOf("query", "检测", "无参数",null));
            commands.add(CommandEntry.valueOf("value", "开度", "有参数",paramJson));
            commands.add(CommandEntry.valueOf("valuePulse", "设置脉冲", "有参数",valueJson));
            commands.add(CommandEntry.valueOf("queryPulse", "读取脉冲", "无参数",null));
            commands.add(CommandEntry.valueOf("queryVersion", "读版本号", "无参数",null));
            commands.add(CommandEntry.valueOf("reboot", "重启", "无参数",null));
            return commands;
        }

        public IDevice createDevice(ICenterFacilities iCenterFacilities, DeviceEntity entity) {
            return new SolarRollFilm4GV2Device(ZhnyModBusServer.getInstance().getEngine () ,this,iCenterFacilities,entity);
        }

        public Class<? extends StoreEntity> storeClass() {
            return SolarRollFilm4GV2StoreEntry.class;
        }

        public String getId() {
            return "zhny-4g-solar-roll-film-v2-plugin";
        }

        public String getName() {
            return "智慧农业4G太阳能卷膜器V2插件";
        }

        public FactoryType getType() {
            return FactoryType.RollFilm;
        }
    }
}
